"""
博客编辑发布页测试
"""
import time
from telnetlib import EC

from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from Utils.Utils import ForumDriver


class Edit:
    url = ""
    driver = ""

    def __init__(self):
        self.url = "http://127.0.0.1:58080/index.html"
        self.driver = ForumDriver.driver
        self.driver.get(self.url)
        self.driver.fullscreen_window()
        self.driver.execute_cdp_cmd("Emulation.setPageScaleFactor", {
            "pageScaleFactor": 0.6  # 60%缩放
        })

    # 成功发布
    def PublishSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        # 输入正确的密码和账号
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("user")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        # time.sleep(2)
        # 点击登录
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 点击“发布帖子”按钮进入博客编辑发布页面
        self.driver.find_element(By.CSS_SELECTOR, '#bit-forum-content > div.page-header.d-print-none > div > div > '
                                                  'div.col-auto.ms-auto.d-print-none > div > '
                                                  'a.btn.btn-primary.d-none.d-sm-inline-block.article_post').click()
        # 输入标题和内容
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_title").send_keys("new title")
        # 定位到标题位置，然后tab到内容输入框内
        title_table = self.driver.find_element(By.CSS_SELECTOR, "#article_post_title")
        title_table.send_keys(Keys.TAB)
        ele = self.driver.switch_to.active_element
        # 定位完成，输入内容
        ele.send_keys("这个是内容")
        # time.sleep(2)
        # 定位提交按钮并滚动到该元素
        submit_button = self.driver.find_element(By.CSS_SELECTOR, "#article_post_submit")
        self.driver.execute_script("arguments[0].scrollIntoView();", submit_button)
        time.sleep(3)
        submit_button.click()
        time.sleep(2)
        # 查找跳转页面的元素，查看博客标题和时间
        text = self.driver.find_element(By.CSS_SELECTOR, 'body > div.jq-toast-wrap.bottom-right > div').text
        print(text)

        ForumDriver.GetScreenShot()

    def PublishFailTest(self):
        # 点击“发布帖子”按钮进入博客编辑发布页面
        self.driver.find_element(By.CSS_SELECTOR, '#bit-forum-content > div.page-header.d-print-none > div > div > ''div.col-auto.ms-auto.d-print-none > div > ''a.btn.btn-primary.d-none.d-sm-inline-block.article_post').click()
        # 输入过长的标题
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_title").send_keys("new title" * 100)
        # tab定位到内容框
        title_table = self.driver.find_element(By.CSS_SELECTOR, "#article_post_title")
        title_table.send_keys(Keys.TAB)
        ele = self.driver.switch_to.active_element
        # 定位完成，输入内容
        ele.send_keys("这个是内容")
        # 定位提交按钮并滚动到该元素
        submit_button = self.driver.find_element(By.CSS_SELECTOR, "#article_post_submit")
        self.driver.execute_script("arguments[0].scrollIntoView();", submit_button)
        time.sleep(3)
        submit_button.click()
        time.sleep(2)
        # 查找警告弹窗
        text = self.driver.find_element(By.CSS_SELECTOR, 'body > div.jq-toast-wrap.bottom-right > div').text
        print(text)

# edit = Edit()
# edit.PublishSucTest()
